package com.backend.modules.system.controller;

import com.backend.common.model.R;
import com.backend.modules.system.convert.UserConvert;
import com.backend.modules.system.dto.StudentDTO;
import com.backend.modules.system.dto.StudentQueryDTO;
import com.backend.modules.system.service.StudentService;
import com.backend.modules.system.vo.UserVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "学生管理", description = "学生管理相关接口")
@RestController
@RequestMapping("/v1/students")
@RequiredArgsConstructor
public class StudentController {

    private final StudentService studentService;

    @Operation(summary = "创建学生")
    @PostMapping
    @PreAuthorize("hasRole('ADMIN')")
    public R<Void> create(@Validated @RequestBody StudentDTO dto) {
        studentService.create(dto);
        return R.ok();
    }

    @Operation(summary = "更新学生")
    @PutMapping("/{id}")
    @PreAuthorize("hasRole('ADMIN')")
    public R<Void> update(@PathVariable Long id, @Validated @RequestBody StudentDTO dto) {
        studentService.update(id, dto);
        return R.ok();
    }

    @Operation(summary = "获取学生详情")
    @GetMapping("/{id}")
    @PreAuthorize("hasRole('ADMIN')")
    public R<UserVO> getById(@PathVariable Long id) {
        return R.ok(UserConvert.convert(studentService.getById(id)));
    }

    @Operation(summary = "分页查询学生")
    @GetMapping("/page")
    @PreAuthorize("hasRole('ADMIN')")
    public R<Page<UserVO>> page(
            @Schema(description = "学号") @RequestParam(required = false) String userCode,
            @Schema(description = "姓名") @RequestParam(required = false) String realName,
            @Schema(description = "班级ID列表") @RequestParam(required = false) List<Long> departmentIds,
            @ParameterObject Pageable pageable) {
        StudentQueryDTO query = new StudentQueryDTO();
        query.setUserCode(userCode);
        query.setRealName(realName);
        query.setDepartmentIds(departmentIds);
        return R.ok(studentService.page(query, pageable).map(UserConvert::convert));
    }

    @Operation(summary = "删除学生")
    @DeleteMapping("/{id}")
    @PreAuthorize("hasRole('ADMIN')")
    public R<Void> delete(@PathVariable Long id) {
        studentService.delete(id);
        return R.ok();
    }

    @Operation(summary = "更新学生状态")
    @PutMapping("/{id}/status")
    @PreAuthorize("hasRole('ADMIN')")
    public R<Void> updateStatus(
            @PathVariable Long id,
            @Schema(description = "状态") @RequestParam Boolean status) {
        studentService.updateStatus(id, status);
        return R.ok();
    }

    @Operation(summary = "重置学生密码")
    @PutMapping("/{id}/password/reset")
    @PreAuthorize("hasRole('ADMIN')")
    public R<Void> resetPassword(@PathVariable Long id) {
        studentService.resetPassword(id);
        return R.ok();
    }
} 